package com.ghpt.controller;

import com.ghpt.config.DynamicDataSourceConfig;
import com.ghpt.service.DataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/db")
public class DataSourceController {

    @Autowired
    private DataSourceService dataSourceService;

    @GetMapping("/list")
    public List<String> listDatabases() throws SQLException {
        return dataSourceService.listAllDatabases();
    }

    @PostMapping("/create")
    public String createDatabase(@RequestParam String newName, @RequestParam String fromDb) throws SQLException {
        dataSourceService.createNewDatabase(newName, fromDb);
        return "Database created.";
    }

    @PostMapping("/switch")
    public String switchDatabase(@RequestParam String dbName) {
        DynamicDataSourceConfig.switchDataSource(dbName);
        return "Switched to: " + dbName;
    }
}